﻿using Sidvall.Security;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading.Tasks;

namespace Sidvall.Web.Security
{
    public class UserAuthenticationMethodValidator : IIdentityValidator
    {
        #region Public Members

        #region AuthenticationMethod

        public string AuthenticationMethod
        {
            get
            {
                return this.Validator.AuthenticationMethod;
            }
            set
            {
                this.Validator.AuthenticationMethod = value;
            }
        }

        #endregion
        #region IgnoredUsers

        public System.Collections.ObjectModel.Collection<string> IgnoredUsers
        {
            get
            {
                return this.Validator.IgnoredUsers;
            }
        }

        #endregion

        #region ValidateAsync

        public Task<IdentityValidationResult> ValidateAsync(IIdentity identity)
        {
            if (this.Validator.IsValid(identity, System.Web.HttpContext.Current.Request.Form))
            {
                return Task.FromResult(new IdentityValidationResult()
                {
                    IdentityStatus = Sidvall.Security.IdentityStatus.Valid,
                });
            }
            else
            {
                return Task.FromResult(new IdentityValidationResult()
                {
                    IdentityStatus = Sidvall.Security.IdentityStatus.RequiresVerification,
                });
            }
        }

        #endregion

        #endregion
        #region Private Members

        private AuthenticationMethodValidator Validator { get; set; }

        #endregion
        #region Constructors

        public UserAuthenticationMethodValidator()
            : this(null, null)
        {
        }
        public UserAuthenticationMethodValidator(string authenticationMethod)
            : this(authenticationMethod, null)
        {
        }
        public UserAuthenticationMethodValidator(string authenticationMethod, IEnumerable<string> ignoredUsers)
        {
            this.Validator = new AuthenticationMethodValidator(authenticationMethod, ignoredUsers);
        }

        #endregion
    }
}
